
/**
 * 
 * @author Simon Auner 2011-11-28
 * @param {Object} cfg The config object
 * @param {String} [cfg.msg] If provided, will set message text 
 * @param {Object[]} [cfg.buttons] Array of buttons
*/
function MessageBox(cfg) {
    
    if(typeof messageBoxHandler === "undefined") {
        messageBoxHandler = new MessageBoxHandler();
    }
    
    
    
    var me = this,
    msg = document.createElement("div"),
    textContainer,
//    appendChild = msg.appendChild,
    type = cfg.type || "info",
    buttons = cfg.buttons;
    
    
    me.messageBox = msg;
    msg.className = "messageBox alert-message block-message " + type;
    
    
    
    
    if(typeof cfg.msg !== "undefined") {
        textContainer = msg.appendChild(document.createElement("div"));
        textContainer.className = "textContainer";
        textContainer.innerText = cfg.msg;
        me.textContainer = textContainer;
        
    }else if(typeof cfg.el !== "undefined") {
        me.elContainer = msg.appendChild(cfg.el);
    }
    
    
    me.buttonContainer = msg.appendChild(document.createElement("div"));
    me.buttonContainer.className = "buttonContainer";
    

    
    me.animationDuration = 300;
    
    if(typeof buttons !== "undefined") {
        me.createButtons(buttons);
    }
    
    me.quickHide();
    
    messageBoxHandler.add(me);
    
    
    
//    width : 300px;
    
}


MessageBox.prototype = {


/**
 * Adds buttons to the message box
 */
    createButtons : function(arr) {
        var me = this,
         
        
        i, l = arr.length, obj,
        // calculate the button width, take care of spacing in between buttons
        buttonWidth = (me.myWidth - (l - 1) * 5) / l;
        
        for(i = 0; i < l; i++) {
            
            obj = arr[i];
            obj.width = buttonWidth;
            
            me.buttonContainer.appendChild(new Button(arr[i]));
        }
        
    },


/**
 * @author Simon Auner 2011-11-28
 * @param {String} msg New message
 */
    setMessage : function(msg) {
        this.textContainer.innerText = msg;
    },

/**
 * Shows the message box
 * @author Simon Auner 2011-11-28
 */
    show : function(callback) {
        messageBoxHandler.show(this, callback);
    },
    
    showInDom : function(callback) {
        var me = this,
        el = me.messageBox,
        body = document.body,
        style = el.style;
        
        style[$.btn] = $.bt + " " + me.animationDuration + "ms ease-in-out";
        
        
        
        
        body.appendChild(el);
        
        
        
        
        setTimeout(function() {
            

            var clientWidth = body.offsetWidth,
            hCenter = Math.round(clientWidth) / 2,
            leftPos = hCenter - el.offsetWidth / 2,
            
            clientHeight = 550,
            vCenter = Math.round(clientHeight) / 2;
            
            
            style.left = leftPos + "px";
            
            me.topPos = vCenter - el.offsetHeight / 2;
            style.top = me.topPos + "px";
            
            
        }, 10);
        
        if(typeof callback !== "undefined") {
            var fn = function() {
                el.removeEventListener($.bte, fn, false);
                callback();
            }
            el.addEventListener($.bte, fn, false);
        }
        
        setTimeout(function() {
            style[$.bt] = "translateY(0)";
        }, 50);
    },


    hide : function() {
        messageBoxHandler.hide(this);
        
    },

/**
 * Hide the message box
 * @author Simon Auner 2011-11-28
 */
    hideInDom : function(callback) {
        var me = this,
        el = this.messageBox;
        el.style[$.btn] = $.bt + " " + me.animationDuration + "ms ease-in-out";
        
        setTimeout(function() {
            el.style[$.bt] = "translateY(-" + ((me.topPos || 250) + 500) + "px)";
            if(typeof callback !== "undefined") {
                callback();
            }
        }, 10);
        
        
        
    },
    
/**
 * Hide the message box
 * @author Simon Auner 2011-11-28
 */
    quickHide : function() {
        var me = this,
        el = this.messageBox,
        style = el.style;
        style[$.btn] = $.bt + " 0ms";
        
        setTimeout(function() {
            style[$.bt] = "translateY(-" + ((me.topPos || 250) + 500) + "px)";
        }, 10);
        
    },
    
/**
 * Shows the message box
 * @author Simon Auner 2011-11-28
 */
    quickShow: function() {
        var el = this.messageBox,
        style = el.style;
        
        style[$.btn] = $.bt + " 0ms";
        
        setTimeout(function() {
            style[$.bt] = "translateY(0)";
        }, 10);
        
    }
    
    
};